<?php

namespace App\Http\Controllers\Admin;

use App\Http\Services\RoleService;
use App\Http\Services\RuleService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class RuleController extends Controller
{

    /**
     *权限列表
     * @author: lican<364548233@qq.com>
     */
    public function index(Request $request){
        $this->validate($request, [
            'admin' => 'required'
        ]);
        $page = (int)$request->input('page') ?? 1;
        $pageSize = (int)$request->input('pageSize') ?? 10;
        $keyword = trim($request->input('keyword')) ?? '';
        $res = RuleService::index($keyword, $page, $pageSize);
        return $res;
    }

    /**
     *权限添加
     * @author: lican<364548233@qq.com>
     */
    public function addRule(Request $request){
        $this->validate($request, [
            'admin' => 'required',
            'name' => 'required',
            'url' => 'required',
            'parent_id' => 'required|int'
        ]);
        $admin = $request->input('admin');
        $id = (int)$request->input('id') ?? 0;
        $name = trim($request->input('name'));
        $url = trim($request->input('url'));
        $icon = trim($request->input('icon')) ?? '';
        $parentId = (int)$request->input('parent_id');
        $res = RuleService::addRule($admin, $name, $url, $parentId, $icon, $id);
        return $res;
    }

    /**
     *权限删除
     * @author: lican<364548233@qq.com>
     */
    public function deleteRole(Request $request){
        $this->validate($request, [
            'admin' => 'required',
            'id' => 'required|int'
        ]);
        $admin = $request->input('admin');
        $id = (int)$request->input('id');
        $res = RuleService::deleteRole($admin, $id);
        return $res;
    }

    /**
     *权限状态切换
     * @author: lican<364548233@qq.com>
     */
    public function changeStatus(Request $request){
        $this->validate($request, [
            'admin' => 'required',
            'id' => 'required|int'
        ]);
        $admin = $request->input('admin');
        $id = (int)$request->input('id');
        $res = RuleService::changeStatus($admin, $id);
        return $res;
    }
}
